package cn.gz.demo.ui.task;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import cn.gz.demo.R;
import cn.gz.demo.databinding.MsgQueueBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.LogUtil;

public class MsgQueueActivity extends BaseActivity {
    private MsgQueueBinding binding;
    private int highIndex, defIndex, lowIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_msg_queue);

        new LogTask("默认优先级任务").setPriority(TaskPriority.DEFAULT).enqueue();
        findViewById(R.id.btn_high).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++highIndex;
                new WorkTask(highIndex + "高优先级 任务").setPriority(TaskPriority.HIGH).doTask();
                ++highIndex;
                new WorkTask(highIndex + "高优先级 任务").setPriority(TaskPriority.HIGH).doTask();
                ++highIndex;
                new WorkTask(highIndex + "高优先级 任务").setPriority(TaskPriority.HIGH).doTask();
            }
        });
        findViewById(R.id.btn_def).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++defIndex;
                new LogTask(defIndex + "默认优先级任务").setPriority(TaskPriority.DEFAULT).doTask();
            }
        });
        findViewById(R.id.btn_low).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++lowIndex;
                new LogTask(lowIndex + "低优先级任务").setPriority(TaskPriority.LOW).doTask();
            }
        });
    }

    class WorkTask extends BaseTask {
        String name;

        public WorkTask(String name) {
            this.name = name;
        }

        //执行任务方法，在这里实现你的任务具体内容
        @Override
        public void doTask() {
            super.doTask();
            LogUtil.showLog("LogTask", "--doTask-" + name);

            for (int i = 0; i < 200; i++) {
                LogUtil.showLog("......" + name + "......." + i);
            }
        }

        //任务执行完的回调，在这里你可以做些释放资源或者埋点之类的操作
        @Override
        public void finishTask() {
            super.finishTask();
            LogUtil.showLog("LogTask", "--finishTask-" + name);
        }
    }
}
